package com.study.web

import javax.annotation.Resource
import javax.servlet.http.{HttpServletRequest, HttpServletResponse}
import org.apache.commons.io.{FileUtils, IOUtils}
import org.slf4j.LoggerFactory
import org.springframework.core.env.Environment
import org.springframework.stereotype.Controller
import org.springframework.util.StringUtils
import org.springframework.web.bind.annotation.{PathVariable, RequestMapping}

/**
 *
 * @version 1.0
 * @author study
 * @date 2020/9/13 8:18
 *
 */
@Controller
private class PicController @Resource()(val request: HttpServletRequest, val resp: HttpServletResponse, val environment: Environment) {
  //  TODO 日志记录器
  val log = LoggerFactory.getLogger(getClass)

  /**
   * 打包时 配置文件分开 这样也可获取资源路劲
   *
   * @param ids
   */
  @RequestMapping(Array("showpic/{ids}"))
  def showPicByIds(@PathVariable ids: String, resp: HttpServletResponse): Unit = {
    log.info(s"查找的picid ${ids}")
    import org.springframework.core.io.ClassPathResource
    val resource = new ClassPathResource(s"pic/${ids}.jpg")
    IOUtils.copy(FileUtils.openInputStream(resource.getFile()), resp.getOutputStream)
  }

  @RequestMapping(Array("edu/{ids}"))
  def edu(@PathVariable ids: String, response: HttpServletResponse): Unit = {
    import org.springframework.core.io.ClassPathResource
    log.info(s"${ids}")
    var idss = ids
    if (StringUtils.isEmpty(idss)) {
      idss = "1"
    }
    val resource = new ClassPathResource(s"pic/${idss}.jpg")
    IOUtils.copy(FileUtils.openInputStream(resource.getFile()), resp.getOutputStream)
  }
}
